package com.vcredit.creditcat.service;
import com.vcredit.creditcat.model.ProductApiUrl;
import com.vcredit.creditcat.persistence.base.ProductApiUrlMapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
/**
 * 
 **/
@Service
public class ProductApiUrlService {
	@Autowired
    private ProductApiUrlMapper mapper;
    
	@CacheEvict(value = "default")
    public void insert(ProductApiUrl obj){
    	mapper.insert(obj);
    }
    
    @CacheEvict(value = "default")
    public void update(ProductApiUrl obj){
    	mapper.update(obj);
    }
    
    public List<ProductApiUrl> findAll(){
		return mapper.findAll();
	}
    
    @Cacheable(key = "'productApiUrl'", value = "default")
    public Map<Long,Map<Integer,ProductApiUrl>> findAllForMap() {
    	Map<Long,Map<Integer,ProductApiUrl>> result = new HashMap<Long,Map<Integer,ProductApiUrl>>();
    	List<ProductApiUrl> all = this.findAll();
    	for (ProductApiUrl productApiUrl : all) {
    		Map<Integer,ProductApiUrl> map = null;
    		if(result.get(productApiUrl.getProductId().longValue()) == null) {
    			map = new HashMap<Integer,ProductApiUrl>();
    		} else {
    			map = result.get(productApiUrl.getProductId().longValue());
    		}
    		map.put(productApiUrl.getType(), productApiUrl);
    		
    		result.put(productApiUrl.getProductId(), map);
		}
    	return result;
    }
	
	private List<ProductApiUrl> find(ProductApiUrl obj){
		return mapper.find(obj);
	}
}